# coding: utf-8
from typing import Union

from PySide6.QtCore import Qt
from PySide6.QtGui import QIcon, QColor
from PySide6.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout
from qfluentwidgets import IconWidget, BodyLabel, CaptionLabel, FluentIconBase

from components import ViewScrollArea, GridLayout, StreamCardWidget
from .plugins import PluginBase


class PluginCard(StreamCardWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.horizontalLayout = QHBoxLayout(self)
        self.verticalLayout = QVBoxLayout()

        self.iconWidget = IconWidget(self)
        self.titleLabel = BodyLabel(self)
        self.descriptionLabel = CaptionLabel(self)

        self.setFixedHeight(80)
        self.setMinimumSize(300, 80)
        self.iconWidget.setFixedSize(50, 50)
        self.titleLabel.setAlignment(Qt.AlignmentFlag.AlignLeft)
        self.descriptionLabel.setAlignment(Qt.AlignmentFlag.AlignLeft)
        self.descriptionLabel.setWordWrap(True)
        self.descriptionLabel.setTextColor(QColor(100, 100, 100), QColor(150, 150, 150))

        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.addWidget(self.titleLabel)
        self.verticalLayout.addSpacing(5)
        self.verticalLayout.addWidget(self.descriptionLabel)

        self.horizontalLayout.setContentsMargins(16, 15, 16, 15)
        self.horizontalLayout.setSpacing(0)
        self.horizontalLayout.addWidget(self.iconWidget)
        self.horizontalLayout.addSpacing(10)
        self.horizontalLayout.addLayout(self.verticalLayout)

    def setTitle(self, title: str):
        self.titleLabel.setText(title)

    def setDescription(self, description: str):
        self.descriptionLabel.setText(description)

    def setIcon(self, icon: Union[str, QIcon, FluentIconBase]):
        self.iconWidget.setIcon(icon)


class PluginsListWidget(ViewScrollArea):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.gridLayout = GridLayout(self.view)

        self.setObjectName('PluginsListWidget')
        self.gridLayout.setContentsMargins(11, 11, 11, 11)
        self.gridLayout.setItemMinimumWidth(320)

    def addPlugin(self, plugin_card: Union[PluginBase, QWidget], clacked=None):
        card = PluginCard(self.view)
        card.setTitle(plugin_card.NAME)
        card.setDescription(plugin_card.DESCRIPTION)
        card.setIcon(plugin_card.ICON)
        if clacked:
            card.clicked.connect(clacked)
        self.gridLayout.addWidget(card)
